package com.zt.ftphelper.view;

import com.zt.ftphelper.Config;
import com.zt.ftphelper.Main;
import com.zt.ftphelper.util.SimpleAlert;
import com.zt.ftphelper.util.Tools;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

import java.io.*;

/**
 * @author zt
 * @Description:
 * @date 23/08/2019 18:33
 */
public class RootLayoutController {

    private Main main;

    @FXML
    private Button executeBtn;

    @FXML
    private Label footer;

    private File outputFile;

    @FXML
    private void initialize() {

        executeBtn.setDisable(true);
    }

    public void setFooter(String text) {
        footer.setText(text);
    }

    @FXML
    private void openSettingsDialog(){
        main.showSettingsDialog();
    }

    @FXML
    private void generateFile() {
        if (Config.getInstance().getTaskData().size() == 0) {
            new SimpleAlert("任务列表为空");
            return;
        }
        outputFile = Tools.generateFile();
        if (outputFile.exists()) {
            executeBtn.setDisable(false);
        }
    }

    @FXML
    private void executeFlashFxp() {
        File file = new File(Config.FLASHFXP_LOCATION);
        if(!file.exists()){
            new SimpleAlert("请正确配置flashFxp地址");
        }

        String[] parm = {"\""+outputFile.getAbsolutePath()+"\""};

        Process p;
        try
        {
            //执行命令
            p = Runtime.getRuntime().exec(file.getPath()+" \""+outputFile.getAbsolutePath()+"\"");

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

    public void setMain(Main main) {
        this.main = main;
    }
}
